package com.ebt.m.policy.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.data.entity.ProductTag;
import com.ebt.m.data.rxModel.apibean.ProductItemsBean;
import com.ebt.m.utils.DateUtils;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;
import com.jakewharton.rxbinding2.view.RxView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * @author Damon.Han
 */
public class BrandsProductViewItem extends IView {

    private ProductItemsBean currentProduct;

    private TextView mName, cName, mDescription;
    private ImageView mThumbnail;
    private LinearLayout mLabelContainer;
    private RelativeLayout mContent;

    /**
     * @param context
     */
    public BrandsProductViewItem(Context context) {
        super(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_item_product, this);
        mThumbnail = (ImageView) findViewById(R.id.wiki_product_thumbnail);
        mName = (TextView) findViewById(R.id.wiki_product_name);
        cName = (TextView) findViewById(R.id.cname);
        mDescription = (TextView) findViewById(R.id.wiki_product_description);
        mLabelContainer = (LinearLayout) findViewById(R.id.label_container);
        mContent = (RelativeLayout) findViewById(R.id.content);
        RxView.clicks(mContent)
                .throttleFirst(400, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    gotoInsurance();
                });

    }

    private void gotoInsurance() {
        try {
            Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "在线投单");
            bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
            bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, true);
            bundle.putString(WebBaseActivity.WEB_TYPE_LINK, currentProduct.getInsuranceUrl());
            webIntent.putExtras(bundle);
            getContext().startActivity(webIntent);
        } catch (Exception e) {
            Logger.e(e);
        }
    }


    @Override
    public void update(Object... args) {
        ProductItemsBean product = (ProductItemsBean) args[0];
        currentProduct = product;
        mName.setText(product.getName());
        if (!TextUtils.isEmpty(product.getBrandShortName())) {
            cName.setVisibility(VISIBLE);
            cName.setText(product.getBrandShortName());
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(product.getCategoryName() + " | " + product.getAccMinAgeName() + "-" + product.getAccMaxAgeName());
        mDescription.setText(stringBuffer.toString());
        String imgUrl = product.getThumbnail();
        ImageLoaderUtil.getInstance().loadImage(getContext(), new ImageLoader.Builder().url(imgUrl).imgView(mThumbnail).build());
        drawLabels(product.getTagInfo());
    }


    /**
     * 返回产品主键
     *
     * @return
     */
    public int getProductId() {
        return currentProduct.getProductId();
    }


    public void drawLabels(List<ProductTag> tagsBeens) {
        mLabelContainer.removeAllViews();
        if (tagsBeens == null || tagsBeens.size() <= 0) {
            return;
        }
        try {
            GradientDrawable myGrad;
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.rightMargin = 15;
            lp.topMargin = 15;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
            ProductTag tagsBean = null;
            for (int i = 0; i < tagsBeens.size(); i++) {
                tagsBean = tagsBeens.get(i);
                final String tagName = tagsBean.getTagName();
                final String startTime = tagsBean.getTagStartTime();
                final String endTime = tagsBean.getTagEndTime();
                final String url = tagsBean.getTagUrl();
                final String textColor = tagsBean.getForeColor();
                final String bgColor = tagsBean.getBackColor();
                Date dStart = dateFormat.parse(startTime);
                Date dEnd = dateFormat.parse(endTime);
                Date dCurrent = DateUtils.getCurrentTimeDate();
                if (dCurrent.after(dStart) && dCurrent.before(dEnd)) {
                    TextView t = new TextView(getContext());
                    t.setText(tagName);
                    t.setTextSize(10);
                    t.setGravity(Gravity.CENTER);
//                    t.setTextColor(Color.parseColor("#ff858585"));
                    t.setTextColor(Color.parseColor(bgColor));
                    mLabelContainer.addView(t, lp);
                    t.setBackgroundResource(R.drawable.wiki_label_bg);
                    myGrad = (GradientDrawable) t.getBackground();
                    myGrad.setStroke(2, Color.parseColor(bgColor));
                    if (url != null && url.length() > 0) {
                        t.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                Uri uri = Uri.parse(url);
                                Intent it = new Intent(Intent.ACTION_VIEW, uri);
                                getContext().startActivity(it);
                            }
                        });
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
